﻿<?php
	include '../templates/db/db.php';
?>
<div id="information">
	<div id='terminwrite'>
			<div class='terminBoxx'>
				<form method='post' action='' class="form">
				    <h3 class="form_h3">
					Термин:
					</h3>
					<input type='text' name='termin' id='termin' value="<?php echo $_POST['termin']; ?>" />
					<h3 class="form_h3">
					Описание:
					</h3>
					<input type='text' name='opisanie' id='opisanie' value="<?php echo $_POST['opisanie']; ?>" />
				    <h3 class="form_h3">
					Категория:
					</h3>
					<p>
					<select name="cat" style="margin-left: 15px;">
					   <option value="А">А</option>
					   <option value="Б">Б</option>
					   <option value="В">В</option>
					   <option value="Г">Г</option>
					   <option value="Д">Д</option>
					   <option value="Е">Е</option>
					   <option value="Ж">Ж</option>
					   <option value="З">З</option>
					   <option value="И">И</option>
					   <option value="Й">Й</option>
					   <option value="К">К</option>
					   <option value="Л">Л</option>
					   <option value="М">М</option>
					   <option value="Н">Н</option>
					   <option value="О">О</option>
					   <option value="П">П</option>
					   <option value="Р">Р</option>
					   <option value="С">С</option>
					   <option value="Т">Т</option>
					   <option value="У">У</option>
					   <option value="Ф">Ф</option>
					   <option value="Х">Х</option>
					   <option value="Ц">Ц</option>
					   <option value="Ч">Ч</option>
					   <option value="Ш">Ш</option>
					   <option value="Щ">Щ</option>
					   <option value="Ю">Ю</option>
					   <option value="Я">Я</option>
					</select>
					</p>
					<h3 class="form_h3">
					Самоучител:
					</h3>
					<select name="tut" style="margin-left: 15px;">
					<?php 
					$sel_tut = mysql_query('SELECT * FROM tutorials');
					while($row_tut = mysql_fetch_assoc($sel_tut)){
					?>
					<option value="<?php echo $row_tut['name']; ?>"><?php echo $row_tut['name']; ?></option>
					<?php
					}
					?>
					</select>
					<input type='submit' name='submit' id='submit_ajax' value='Запиши' style="margin-top: 5px; margin-left: 15px;"/>
				</form>
				<?php
				   if($_POST['submit']!=true) echo '';
				   else {
					$termin = htmlspecialchars(trim($_POST['termin']));
					$opisanie = htmlspecialchars(trim($_POST['opisanie']));
					$date = time();
					$username = $_SESSION['username'];
					$to = $_POST['tut'];
					$cat = $_POST['cat'];
					
					$select_type = mysql_query('SELECT * FROM users WHERE username="'.$username.'"');
					$type_fetch = mysql_fetch_assoc($select_type);
					
					$type = $type_fetch['type'];
					
					$select_termin = mysql_query('SELECT * FROM termin_user WHERE termin="'.$termin.'"');
					$broi=mysql_num_rows($select_termin);
					
					if($broi == 0 && strlen($termin) > 1){
					   $db_res= 'INSERT INTO termin_user(`username`, `termin`, `opisanie`, `date`, `type_user`, `to_tutorial`, `cat`) 
							VALUES("'.$username.'", "'.$termin.'", "'.$opisanie.'", "'.$date.'", "'.$type.'", "' . $to . '", "' . $cat . '")' or die(mysql_error());
						$db_res_q = mysql_query($db_res) or die(mysql_error());
					} else {
					?>
						<script type='text/javascript'>
							alert('Този термин е под 2 символа или съществува !');						
						</script>
					<?php
					}
					}
				?>
			</div>
			
			<br />
			<hr width='90%' />
			<br />
			
			<div id='termin_Box'></div>
			
	</div>
</div>